#include "Allegro_initialize.h"
#include "game.h"
#include <string>
using namespace std;

void draw_all(Game &game);
void draw_sand(Map &map);
void draw_object(Movables &movable); 
string enum_to_string(Action t);

int main(void)
{
	//------------------------------------------
	//Initialise the Resource Stack
	//------------------------------------------
	
	Allegro_resources resources = {0,0,0};

	SETUP_STATUS  status = init_alleg(resources);
	string error_message = create_error_message(status);
	
	if (status != SETUP_SUCCESFULL)
	{
		display_setup_error_message(error_message);
	}
	//------------------------------------------

	//------------------------------------------
	//Declare Local Variables
	//------------------------------------------
	
	//Initiate the Game Object (Contains all other objects!)
	Game game;
	
	//Set up the user input keys
	Action action = MOVE_NONE;
	bool arrow_keys[4] = {false, false, false, false};
	bool shoot_key = false; 

	//Define loop and rendering booleans
	bool is_done = false;
	bool is_redraw = true;
	bool is_game_over = false;
	
	//------------------------------------------
	//Enter the Game loop
	//------------------------------------------
	al_start_timer(resources._timer);
	while(!is_done)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(resources._event_queue, &ev);

		if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_UP:
					arrow_keys[UP] = true;
					action = MOVE_UP;
					break;
				case ALLEGRO_KEY_DOWN:
					arrow_keys[DOWN] = true;
					action = MOVE_DOWN;
					break;
				case ALLEGRO_KEY_RIGHT:
					arrow_keys[RIGHT] = true;
					action = MOVE_RIGHT;
					break;
				case ALLEGRO_KEY_LEFT:
					arrow_keys[LEFT] = true;
					action = MOVE_LEFT;
					break;
				case ALLEGRO_KEY_SPACE:
					shoot_key = true;
					action = SHOOT;
				
					break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch(ev.keyboard.keycode)
			{
				case ALLEGRO_KEY_UP:
					arrow_keys[UP] = false;
					break;
				case ALLEGRO_KEY_DOWN:
					arrow_keys[DOWN] = false;
					break;
				case ALLEGRO_KEY_RIGHT:
					arrow_keys[RIGHT] = false;
					break;
				case ALLEGRO_KEY_LEFT:
					arrow_keys[LEFT] = false;
					break;
				case ALLEGRO_KEY_SPACE:
					shoot_key = false;
					break;
				case ALLEGRO_KEY_ESCAPE:
					is_done = true;
					break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			is_done = true;
		}
		else if(ev.type == ALLEGRO_EVENT_TIMER && !is_game_over)
		{
			if(!arrow_keys[0]&&!arrow_keys[1]&&!arrow_keys[2]&&!arrow_keys[3]&&!shoot_key) 
				{
					action = MOVE_NONE;
				}
			//cout<<"Action is " <<enum_to_string(action)<<endl;
			switch(game.update(action))
			{
			case YOU_WIN: 
				al_clear_to_color(al_map_rgb(0,0,0));
				al_draw_text(resources.font, YELLOW, screen_width/2,screen_height/2, ALLEGRO_ALIGN_CENTRE, "YOU WIN");
				al_flip_display();
				is_game_over = true; 
				break;
			case YOU_DEAD:
				al_clear_to_color(al_map_rgb(0,0,0));
				al_draw_text(resources.font, RED, screen_width/2,screen_height/2, ALLEGRO_ALIGN_CENTRE, "YOU LOSE");
				al_flip_display();
				is_game_over = true; 
				break;
			case IN_PROGRESS:
				is_redraw = true;
				break;

			}
			
		}

		if(is_redraw)// && al_is_event_queue_empty(resources._event_queue))
		{
			al_set_target_bitmap(al_get_backbuffer(resources._display));
			is_redraw = false;

			draw_all(game);
			
			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));
		//--------------------------------------------------------------------------------------------
		}
	}


	al_destroy_event_queue(resources._event_queue);
	al_destroy_timer(resources._timer);
    al_destroy_display(resources._display);
	cout<<"Exiting the program"<<endl;
	cout<<"Exiting the program"<<endl;
	return 0;
}


void draw_all(Game &game)
{
	   
			//Step 1: Draw the sand
		draw_sand(game.get_map());

		//Step 2: Draw digdug
		draw_object(game.get_movable(DIGDUG, 0));

		//Step 3: Draw Monsters
		for(int i = 0; i < game._monsters.size(); i++)
		{
		draw_object(game.get_movable(MONSTER, i));
		}
		//Step 4: Draw Projectiles
		if(game._is_shooting)
		{
		draw_object(game.get_movable(PROJECTILE, 0));
		}
		//Step 5: Draw Rock
}
		

void draw_sand (Map &map)
{
		float pixel_ratio = game_box_size/game_grid_elements;
		float sand_w = al_get_bitmap_width(map.bitmap());
		float sand_h = al_get_bitmap_height(map.bitmap());

			for(int x_loc = 0; x_loc < game_grid_elements; x_loc++)
			{
				for(int y_loc = 0; y_loc < game_grid_elements; y_loc++)
				{
					if(map.element(x_loc,y_loc) == SAND)
					{
						al_draw_scaled_bitmap(map.bitmap(), 0, 0, sand_w, sand_h, game_box_start_x + x_loc*pixel_ratio,  game_box_start_y + y_loc*pixel_ratio, pixel_ratio, pixel_ratio, 0);
					}
					if(map.element(x_loc,y_loc) == DIGDUG)
					{
						al_draw_filled_rectangle(game_box_start_x + x_loc*pixel_ratio,  game_box_start_y + y_loc*pixel_ratio, game_box_start_x + x_loc*pixel_ratio+5,  game_box_start_y + y_loc*pixel_ratio+5, al_map_rgb(0,0,255));
					}
					if(map.element(x_loc,y_loc) == MONSTER)
					{
						al_draw_filled_rectangle(game_box_start_x + x_loc*pixel_ratio,  game_box_start_y + y_loc*pixel_ratio, game_box_start_x + x_loc*pixel_ratio+5,  game_box_start_y + y_loc*pixel_ratio+5, al_map_rgb(255,0,0));
					}
					if(map.element(x_loc,y_loc) == OVERLAP)
					{
						al_draw_filled_rectangle(game_box_start_x + x_loc*pixel_ratio,  game_box_start_y + y_loc*pixel_ratio, game_box_start_x + x_loc*pixel_ratio+5,  game_box_start_y + y_loc*pixel_ratio+5, al_map_rgb(0,255,0));
					}
					if(map.element(x_loc,y_loc) == PROJECTILE)
					{
						al_draw_filled_rectangle(game_box_start_x + x_loc*pixel_ratio,  game_box_start_y + y_loc*pixel_ratio, game_box_start_x + x_loc*pixel_ratio+5,  game_box_start_y + y_loc*pixel_ratio+5, al_map_rgb(0,255,255));
					}
				}
			}
}

void draw_object(Movables &movable)
{
				float pixel_ratio = game_box_size/game_grid_elements;
				float x = movable.x();// - movable.radius() ;
				float y = movable.y();// - movable.radius();
				float size = movable.radius()*2 + 1; 
				
				ALLEGRO_BITMAP *bitmap = movable.bitmap();

				float height = al_get_bitmap_height(bitmap);
				float width = al_get_bitmap_width(bitmap);
				
				Direction dir = movable.direction();
				
				//calculate the direction rotation
				float angle = 0;
				const float pi = 3.14;
				switch(dir)
				{
				case UP:	angle = pi*3/2;	break;
				case DOWN:	angle = pi*1/2;	break;
				case LEFT:	angle = pi;		break;
				case RIGHT:	angle = 0;		break;
				}

				if(!movable.is_ghost())
				{
				al_draw_scaled_rotated_bitmap(
					bitmap, 
					width/2, height/2, 
					game_box_start_x + x*pixel_ratio + 4 ,game_box_start_y +  y*pixel_ratio +4, 
					size*pixel_ratio/width, size*pixel_ratio/height,
					angle,0);
				}
				else
				{
					al_draw_tinted_scaled_rotated_bitmap(
					bitmap, 
					al_map_rgba_f(0.5,0.5, 0.5, 0.5),
					width/2, height/2, 
					game_box_start_x + x*pixel_ratio + 4 ,game_box_start_y +  y*pixel_ratio +4, 
					size*pixel_ratio/width, size*pixel_ratio/height,
					angle,0);
				}

}

string enum_to_string(Action t)
{
      switch(t)
	  {
         case SHOOT:
            return "SHOOT";
			break;
         case MOVE_NONE:
            return "NONE";
			break;
         default:
            return "DIRECTION";
      }
 }